package cn.yinyu.queue.module.ecg.controller.admin.callingscreen;

import cn.hutool.extra.servlet.ServletUtil;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;
import cn.yinyu.queue.module.ecg.controller.admin.checktype.vo.CheckTypePageReqVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.queue.QueueDO;
import cn.yinyu.queue.module.ecg.enums.QueueStatusEnum;
import cn.yinyu.queue.module.ecg.service.callingscreen.CallingScreenService;
import cn.yinyu.queue.module.ecg.service.checktype.CheckTypeService;
import cn.yinyu.queue.module.ecg.service.config.EcgConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.yinyu.queue.framework.common.pojo.CommonResult.error;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

@Tag(name = "叫号屏 ")
@RestController
@RequestMapping("/ecg/screen")
@Validated
@Slf4j
public class CallingScreenController {

    @Resource
    private CallingScreenService callingScreenService;

    @GetMapping("/display-col-info")
    @Operation(summary = "显示列信息")
    @PermitAll
    public CommonResult<Map<Integer, List<String>>> displayColInfo()
    {
        Map<Integer, List<String>> map = callingScreenService.getDisplayColInfo();
        return success( map );
    }

    @GetMapping("/big-screen-data")
    @Operation(summary = "大屏叫号数据")
    @PermitAll
    public CommonResult<Map<Integer, Map<Integer, List<ScreenQueueRespVO>>>> callingData()
    {
        Map<Integer, Map<Integer, List<QueueDO>>> map = callingScreenService.callingData();

        Map<Integer, Map<Integer, List<ScreenQueueRespVO>>> mapVO = new HashMap<>();
        map.keySet().forEach( displayCol -> {
            //log.info("dispayCol: " + displayCol);
            Map<Integer, List<QueueDO>> mapSoltQueueDO = map.get(displayCol);
            Map<Integer, List<ScreenQueueRespVO>> mapSoltQueueVO = new HashMap<>();
            mapSoltQueueDO.keySet().forEach( displaySlot -> {
                //log.info("displaySlot: " + displaySlot);
                mapSoltQueueVO.put(displaySlot, BeanUtils.toBean(mapSoltQueueDO.get(displaySlot), ScreenQueueRespVO.class));
            });

            mapVO.put(displayCol, mapSoltQueueVO);
        });

        return success( mapVO );
    }

    @GetMapping("/room-screen-data")
    @Operation(summary = "诊间屏数据")
    @PermitAll
    public CommonResult<Map<Integer, List<ScreenQueueRespVO>>> callingDataRoom(HttpServletRequest request)
    {
        String reqIp = ServletUtil.getClientIP(request);

        List<Integer> queueStatusList = new ArrayList<>();
        queueStatusList.add(QueueStatusEnum.RECALLED.getStatus());
        queueStatusList.add(QueueStatusEnum.RECALLED_INSTALL.getStatus());
        queueStatusList.add(QueueStatusEnum.READY.getStatus());
        queueStatusList.add(QueueStatusEnum.ONSTAGE.getStatus());
        queueStatusList.add(QueueStatusEnum.RECEIVED.getStatus());
        queueStatusList.add(QueueStatusEnum.INSTALLING.getStatus());
        queueStatusList.add(QueueStatusEnum.PASSED.getStatus());
        queueStatusList.add(QueueStatusEnum.PASSED_INSTALL.getStatus());
        List<QueueDO> queueDOList = callingScreenService.getRoomQueue(reqIp, queueStatusList);

//        List<QueueDO> passedList = queueDOList.stream().filter(queue -> queue.getStatus() == QueueStatusEnum.PASSED.getStatus() || queue.getStatus() == QueueStatusEnum.PASSED_INSTALL.getStatus()).toList();
//        queueDOList.removeIf(queue -> queue.getStatus() == QueueStatusEnum.PASSED.getStatus());

        Map<Integer, List<ScreenQueueRespVO>> mapVO = new HashMap<>();
        mapVO.put(1, BeanUtils.toBean(queueDOList, ScreenQueueRespVO.class));
//        mapVO.put(2, BeanUtils.toBean(passedList, ScreenQueueRespVO.class));
        return success( mapVO );
    }

}
